package cn.vcjf.weapp.api.goods.service.impl;

import cn.vcjf.weapp.api.goods.entity.GoodsDiscount;
import cn.vcjf.weapp.api.goods.mapper.GoodsDiscountMapper;
import cn.vcjf.weapp.api.goods.service.GoodsDiscountService;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Service("goodsDiscountService")
public class GoodsDiscountServiceImpl extends ServiceImpl<GoodsDiscountMapper, GoodsDiscount> implements GoodsDiscountService {

	@Override
	public TableResult addGoodsDiscount(GoodsDiscount goodsDiscount, Integer userId) {
		Integer userDiscount = baseMapper.findUserDiscount(goodsDiscount, userId);
		if(userDiscount!=null && userDiscount>0){
			return TableResult.ErrorResult("你已经领取过该优惠券");
		}
		Integer i = baseMapper.updateGoodsDiscount(goodsDiscount, userId);
		Integer integer = null;
		if(i!=null&&i>0) {
			integer = baseMapper.addUserDiscount(goodsDiscount, userId);
		}
		if(integer != null && integer > 0){
			return TableResult.OKResult("领取成功");
		}
		return TableResult.ErrorResult("领取失败");
	}
}
